package com.cn.kehong.service;


import com.cn.kehong.api.IProductDianciFenjService;
import com.cn.kehong.api.IProductDianciPeriodService;
import com.cn.kehong.dao.ProductDianciPeriodMapper;
import com.cn.kehong.domain.ProductDianciFenj;
import com.cn.kehong.domain.ProductDianciPeriod;
import com.cn.kehong.domain.ProductDianciPeriodExample;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.Constants;
import com.cn.kehong.utils.QueryForm;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ProductDianciPeriodServiceImpl implements IProductDianciPeriodService {

    @Autowired
    private ProductDianciPeriodMapper productDianciPeriodMapper;

    @Autowired
    private IProductDianciFenjService productDianciFenjService;

    @Override
    public List<ProductDianciPeriod> getProductDianciPeriodList(QueryForm queryForm) throws Exception {
        int count = productDianciPeriodMapper.selectProductDianciPeriodsCount(queryForm);
        if(count == 0){
            return Lists.newArrayList();
        }
        List<ProductDianciPeriod> productDianciPeriods = productDianciPeriodMapper.selectProductDianciPeriods(queryForm);
        productDianciPeriods.get(0).setCount(count);
        return productDianciPeriods;
    }

    @Override
    public List<ProductDianciPeriod> getProductDianciPeriodListByStatus(QueryForm queryForm) throws Exception {
        String factoryName = queryForm.getFactoryName();
        String dianciStatus = queryForm.getStatus();
        if(StringUtils.isEmpty(factoryName)||StringUtils.isEmpty(dianciStatus)){
            return Lists.newArrayList();
        }
        ProductDianciPeriodExample example = new ProductDianciPeriodExample();
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andFactoryNameEqualTo(factoryName).andDianciStatusEqualTo(dianciStatus);
        example.setOrderByClause("create_time asc");
        return productDianciPeriodMapper.selectByExample(example);
    }

    @Override
    public String addProductDianciPeriod(ProductDianciPeriod productDianciPeriod) throws Exception {
        ProductDianciPeriodExample example = new ProductDianciPeriodExample();
        example.createCriteria().andPeiIdEqualTo(productDianciPeriod.getPeiId()).andYaoGuanCodeEqualTo(productDianciPeriod.getYaoGuanCode()).andStatusEqualTo(Constants.NORMAL_STATUS);
        int existCount = productDianciPeriodMapper.countByExample(example);
        if(existCount > 0){
            throw new Exception("data has exist");
        }
        String id = CommonUtil.generateRandomNum("dianci-");
        productDianciPeriod.setId(id);
        int count = productDianciPeriodMapper.insertSelective(productDianciPeriod);
        if(count < 1){
            throw new Exception("insert dianchuci data error");
        }
        return "";
    }

    @Override
    public String updateProductDianciToFenji(ProductDianciPeriod productDianciPeriod) throws Exception {
        String id = productDianciPeriod.getId();
        if(StringUtils.isEmpty(id)){
            return "缺少id";
        }
        ProductDianciPeriod exist =  productDianciPeriodMapper.selectDianciPeriodById(id);
        if(null == exist){
            return "电除磁不存在";
        }
        String dianciStatus = exist.getDianciStatus();
        if(!StringUtils.equals(dianciStatus,"1")){
            return "不在电除磁阶段，无法分级";
        }

        if(Integer.parseInt(exist.getFenjiCount())> 2){
            return "分级次数超过2次";
        }

        ProductDianciPeriod update = new ProductDianciPeriod();
        int fenjiCount = Integer.parseInt(exist.getFenjiCount());
        update.setFenjiCount((fenjiCount+1)+"");
        update.setHasFenji("1");
        update.setId(productDianciPeriod.getId());
        ProductDianciPeriodExample example = new ProductDianciPeriodExample();
        example.createCriteria().andIdEqualTo(id);
        int count = productDianciPeriodMapper.updateByExampleSelective(update,example);
        if(count < 1){
            return "更新除磁表异常";
        }
        ProductDianciFenj productDianciFenj = new ProductDianciFenj();
        productDianciFenj.setDianciId(id);
        productDianciFenj.setFactoryName(exist.getFactoryName());
        productDianciFenj.setProductCode(exist.getProductCode());
        productDianciFenj.setFenjiCode(productDianciPeriod.getFenjiCode());
        productDianciFenj.setStartCode(exist.getStartCode());
        productDianciFenj.setFenjiStartTime(productDianciPeriod.getDianciStartTime());
        String resp = productDianciFenjService.addProductDianciFenj(productDianciFenj);
        if(StringUtils.isNotEmpty(resp)){
            throw new Exception(resp);
        }
        return "";
    }

    @Override
    public String updateBatchProductDianciPeriod(List<ProductDianciPeriod> productDianciPeriods) throws Exception {
        return null;
    }

    @Override
    public String updateEndProductDianciPeriod(ProductDianciPeriod productDianciPeriod) throws Exception {
        String id = productDianciPeriod.getId();
        if(StringUtils.isEmpty(id)){
            return "id is null";
        }
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ProductDianciPeriod update = new ProductDianciPeriod();
        update.setDianciEndTime(productDianciPeriod.getDianciEndTime());
        update.setDianciStatus("2");
        ProductDianciPeriodExample example = new ProductDianciPeriodExample();
        example.createCriteria().andIdEqualTo(id);
        int count = productDianciPeriodMapper.updateByExampleSelective(update,example);
        return CommonUtil.outStr(count);
    }

    @Override
    public List<ProductDianciPeriod> getProductDianciPeriodBackList(QueryForm queryForm) throws Exception {
        int count = productDianciPeriodMapper.getProductDianciPeriodBackCount(queryForm);
        if(count == 0){
            return Lists.newArrayList();
        }
        List<ProductDianciPeriod> productDianciPeriods = productDianciPeriodMapper.getProductDianciPeriodBackList(queryForm);
        productDianciPeriods.get(0).setCount(count);
        return productDianciPeriods;
    }
}
